import time


class TokenBucket:
    def __init__(self, rate, capacity):
        self._rate = rate
        self._capacity = capacity
        self._current_token_amount = 0
        self._last_consume_token_time = int(time.time())

    def consume_token(self, token_amount):
        the_increment = self._rate * (int(time.time()) - self._last_consume_token_time)
        self._current_token_amount = min(the_increment + self._current_token_amount, self._capacity)
        if token_amount > self._current_token_amount:
            return False
        self._last_consume_token_time = int(time.time())
        self._current_token_amount -= token_amount
        return True

